﻿namespace ReplayDataFile
{
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;

    public class RecordIndex
    {
        public int Block;
        public int OffsetInBlock;
        public long RecordNo;
        public int RecordNoInKind;
        public int RecordSize;
        public DateTime Time;

        public bool FromByteBuffer(BinaryReader br)
        {
            this.RecordNo = br.ReadInt64();
            this.RecordNoInKind = br.ReadInt32();
            this.Block = br.ReadInt32();
            this.OffsetInBlock = br.ReadInt32();
            this.RecordSize = br.ReadInt32();
            this.Time = new DateTime(br.ReadInt64());
            return true;
        }

        public byte[] ToByteBuffer()
        {
            byte[] buffer = new byte[Size];
            BinaryWriter writer = new BinaryWriter(new MemoryStream(buffer));
            writer.Write(this.RecordNo);
            writer.Write(this.RecordNoInKind);
            writer.Write(this.Block);
            writer.Write(this.OffsetInBlock);
            writer.Write(this.RecordSize);
            writer.Write(this.Time.Ticks);
            return buffer;
        }

        public override string ToString()
        {
            return string.Format("{0}: RecordNo:{1}  Size:{2}  Block:{3}  Offset:{4}", new object[] { this.Time, this.RecordNo, this.RecordSize, this.Block, this.OffsetInBlock });
        }

        public uint FileId { get; set; }

        public static int Size
        {
            get
            {
                return 0x20;
            }
        }
    }
}

